/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.database;

import java.util.ArrayList;
import java.util.List;

import org.niclab.vanbus.application.VanBusApplication;

import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;


public class ApplicationDataBase {
	
	public static final String DATABASE_NAME= "vanbusDatabase.db";
	
	public static final String DATABASE_TABLE_ROUTE="ROUTE";
	public static final String DATABASE_TABLE_STOP="STOP";
	public static final String DATABASE_TABLE_REMINDER="REMINDER";
	
	public static final int DATABASE_VERSION =1;
	
	private static SQLiteOpenHelper openHelper;
	
	public static SQLiteOpenHelper getSQLiteOpenHelper(){
		
		if(openHelper ==null ){
			openHelper = new DBHelper(VanBusApplication.getInstance(),DATABASE_NAME,null, DATABASE_VERSION);
		}
		return openHelper;
		
		
	}
	
	public static void closeDatabase(){
		openHelper.close();
	}
	
	
	public static List<String> createDatabaseSQL(){
		List<String> statements = new ArrayList<String>();;
		
		StringBuilder builder = new StringBuilder();
		
		//create ROUTE TABLE
		builder.append("CREATE TABLE ROUTE (");
		for(int i=0; i<BusRouteDAO.TABLE_ROUTE_COLUMNS.length;i++){
			builder.append(BusRouteDAO.TABLE_ROUTE_COLUMNS[i]);
			builder.append(" ");
			builder.append(BusRouteDAO.TABLE_ROUTE_COLUMNS_DEFS[i]);
			
		}
		
		//append the table constraint
		if(!BusRouteDAO.TABLE_ROUTE_CONSTRAINTS.trim().equals("")){
			builder.append(",");
			builder.append(BusRouteDAO.TABLE_ROUTE_CONSTRAINTS);	
		}
		builder.append(")");
		
		statements.add(builder.toString());
		
		//create STOP TABLE
		builder = new StringBuilder();
		builder.append("CREATE TABLE STOP (");
		
		for(int i=0; i<BusStopDAO.TABLE_STOP_COLUMNS.length; i++){
			builder.append(BusStopDAO.TABLE_STOP_COLUMNS[i]);
			builder.append(" ");
			builder.append(BusStopDAO.TABLE_STOP_COLUMNS_DEFS[i]);
		}
		
		//append the table constraint
		if(!BusStopDAO.TABLE_STOP_CONSTRAINTS.trim().equals("")){
			builder.append(",");
			builder.append(BusStopDAO.TABLE_STOP_CONSTRAINTS);
		}
		builder.append(")");
		
		statements.add(builder.toString());
		
		//creat Reminder Table
		builder = new StringBuilder();
		builder.append("CREATE TABLE REMINDER (");
		
		for(int i=0; i<ReminderDAO.TABLE_REMINDER_COLUMNS.length; i++){
			builder.append(ReminderDAO.TABLE_REMINDER_COLUMNS[i]);
			builder.append(" ");
			builder.append(ReminderDAO.TABLE_REMINDER_COLUMNS_PROPERTIES[i]);
			
		}
		
		//append the table constraint
		if(!ReminderDAO.TABLE_REMINDER_CONSTRAINTS.trim().equals("")){
			builder.append(",");
			builder.append(ReminderDAO.TABLE_REMINDER_CONSTRAINTS);
		}
		
		builder.append(");");
		
		statements.add(builder.toString());
		return statements;
	}
	
	public static String dropDatabaseSQL(){
		StringBuilder builder = new StringBuilder();
		
		builder.append("DROP TABLE IF EXISTS");
		builder.append(DATABASE_TABLE_ROUTE);
		builder.append(";");
		
		builder.append("DROP TABLE IF EXISTS");
		builder.append(DATABASE_TABLE_STOP);
		builder.append(";");
		
		builder.append("DROP_TABLE IF EXISTS");
		builder.append(DATABASE_TABLE_REMINDER);
		builder.append(";");
		
		return builder.toString();
	}

}
